package com.thuanhoang.boo;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import thirdparty.HorizontalListView;
import util.Constant;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.boo.service.SendDataService;
import com.google.gson.Gson;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.thuanhoang.boo.adapter.ChannelListAdapter;
import com.thuanhoang.boo.model.Channel;

public class ChannelActivity  extends FragmentActivity{
	private static ArrayList<Channel> channelList;
	private WebView webChannel;
	

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.channel);
		showLoading() ;
		webChannel = (WebView) findViewById(R.id.fragment2);
		channelList = new ArrayList<Channel>();		
		getChannel();
		initAction();
		
	}

	private void initAction() {
		View btnMap =(View) findViewById(R.id.btn_map);
		btnMap.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ChannelActivity.this,MapActivity.class);
				startActivity(intent);

			}
		});
		
		View btnAccount = (View)findViewById(R.id.btn_account);
		btnAccount.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(ChannelActivity.this, UpdateInfoActivity.class));
			}
		});
		// TODO Auto-generated method stub
		
	}

	public void showLoading() {
		findViewById(R.id.loadingPanel).setVisibility(View.VISIBLE);
	}

	public void hiddenLoading() {
		findViewById(R.id.loadingPanel).setVisibility(View.GONE);
	}
	public void getChannel(){
		SendDataService service = new SendDataService(this);	
		JsonHttpResponseHandler callback = new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(JSONObject response) {
				try{
					Log.d("channel", "ok " + response);
					ArrayList<Channel> channelTmp = new ArrayList<Channel>();
					JSONArray listChannel = response.getJSONArray("Channels");
					if(listChannel!=null){
						Gson gson = new Gson();
						for (int i = 0; i < listChannel.length(); i ++){
							JSONObject memberJsonInfo = listChannel.getJSONObject(i);
							String stringInfo = memberJsonInfo.toString();
							Channel newStore = gson.fromJson(stringInfo, Channel.class);
							channelTmp.add(newStore);
						}
						if(channelList.size()>0 && channelList.size() == channelList.size()){ // should deep compare is better!
							Log.e("Cache", "Not need reload");
							return;
						}
						channelList.clear();
						for(Channel channel: channelTmp){
							channelList.add(channel);
						}
					}
					if(channelList.size()>0){
						updateListchanel();
					}					
				}
				catch(Exception e){
					
				}
				
			}


			@Override
			public void onFailure(Throwable error) {
				Log.d("error", "is " + error.getMessage());
				error.printStackTrace();
			}
		};

		// get user history by using email
		service.getData(Constant.API_LIST_CHANNEL, callback);
	}
	

	private void updateListchanel() {
		HorizontalListView listChannel = (HorizontalListView) findViewById(R.id.listChanel);
		listChannel.setAdapter(new ChannelListAdapter(this,channelList));
		Channel channel = channelList.get(0);
		if(channel.getChannelLink()!=null){
			webChannel.loadUrl(channel.getChannelLink());
		}
		listChannel.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Channel channel = channelList.get(position);
				if(channel.getChannelLink()!=null){
					webChannel.loadUrl(channel.getChannelLink());
				}
			}

		});
		hiddenLoading();
		// TODO Auto-generated method stub
		
	}
	
//	public void afterStore(JSONObject response){
//		ArrayList<Store> storeTmp = new ArrayList<Store>();
//		try {
//			Log.d("Store", " get ok");
//			JSONArray listStore = response.getJSONArray("Stores");
//			if(listStore!=null){
//				Gson gson = new Gson();
//				for (int i = 0; i < listStore.length(); i ++){
//					JSONObject memberJsonInfo = listStore.getJSONObject(i);
//					String stringInfo = memberJsonInfo.toString();
//					Store newStore = gson.fromJson(stringInfo, Store.class);
//					storeTmp.add(newStore);
//				}
//				if(storeList.size()>0 && storeList.size() == storeTmp.size()){ // should deep compare is better!
//					Log.e("Cache", "Not need reload");
//					return;
//				}
//				storeList.clear();
//				for(Store store: storeTmp){
//					storeList.add(store);
//				}
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//	}

	@Override
	protected void onResume() {
		super.onResume();
		//initilizeMap();
	}

}
